package com.itboye.textileexpres.myset;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.textileexpes.beans.OfferLineOrderBeans;
import com.itboye.textileexpes.beans.OfferLineOrderBeans.IdCCCC.Items;
import com.itboye.textileexpes.presenter.MyOrderPresenter;
import com.itboye.textileexpes.presenter.ShopCartPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.R.id;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.util.ClickListener;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.util.TimeFormat;
import com.itboye.textileexpres.util.XImageLoader;
import com.itboye.textileexpres.widget.MyListView;
import com.itboye.textileexpres.widget.XListView;
import com.itboye.textileexpres.widget.XListView.IXListViewListener;
import com.itboye.textileexptes.config.GlobalConfig;

public class ActivityOffLine extends BaseActivity implements OnClickListener,
		IXListViewListener, Observer {
	ImageView back;
	TextView title;
	ImageView img_other;
	OfferLineOrderBeans beans;
	ClickListener listener;
	// OfferLineOrderBeans lineOrderBeans = new OfferLineOrderBeans();
	private int page_size =10;
	private int page_index = 1;
	LinearLayout off_lin;
	XListView listview;
	ShopCartPresenter cartPresenter;
	listAdapter adapter;
	long createtime;
	MyOrderPresenter myOrderPresenter;
	List<Items> items;
	String type = "load";
	LinearLayout null_order;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.acitivity_off;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		title.setText(R.string.lineOrder);
		img_other.setVisibility(View.GONE);
		beans = new OfferLineOrderBeans();
		cartPresenter = new ShopCartPresenter(this);
		cartPresenter.onShopCharOrder(IsMobileNumPhone.isUid(), page_size+"",
				page_index + "");
		myOrderPresenter = new MyOrderPresenter(this);
		items = new ArrayList<OfferLineOrderBeans.IdCCCC.Items>();
		IntentFilter filter = new IntentFilter("offLineOrder");
		registerReceiver(broadcastReceiver, filter);
		listview.setPullLoadEnable(true);
		listview.setPullRefreshEnable(true);
		listview.setXListViewListener(this);

	}

	BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			beans = new OfferLineOrderBeans();
			cartPresenter.onShopCharOrder(IsMobileNumPhone.isUid(), page_size+"",
					page_index + "");
		}
	};

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == ShopCartPresenter.ShopcarOrderSuccessEvent) {
				stop();
				if ("load".equals(type)) {

				} else {
					beans = new OfferLineOrderBeans();
				}
				beans = (OfferLineOrderBeans) entity.getData();
				if (beans == null || beans.getList().size() == 0) {
					listview.setVisibility(View.GONE);
					null_order.setVisibility(View.VISIBLE);
				} else {
					listview.setVisibility(View.VISIBLE);
					null_order.setVisibility(View.GONE);
				}
				adapter = new listAdapter(ActivityOffLine.this, beans);
				listview.setAdapter(adapter);
			} else if (entity.getEventType() == ShopCartPresenter.ShopcarOrderFailureEvent) {
				stop();
				toast(entity.getData() + "");
			}
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;

		default:
			break;
		}
	}

	class listAdapter extends BaseAdapter {
		Context context;
		OfferLineOrderBeans lineOrderBeans;

		public listAdapter(Context context) {
			// TODO Auto-generated constructor stub
			this.context = context;
		}

		public listAdapter(Context context, OfferLineOrderBeans lineOrderBeans) {
			// TODO Auto-generated constructor stub
			this.context = context;
			this.lineOrderBeans = lineOrderBeans;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return lineOrderBeans.getList().size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			viewHolder holder = null;
			if (convertView == null) {
				holder = new viewHolder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.item_offline_expand, null);
				holder.create_time = (TextView) convertView
						.findViewById(R.id.create_time);
				holder.delete_tv = (TextView) convertView
						.findViewById(R.id.delete_tv);
				convertView.setTag(holder);
			} else {
				holder = (viewHolder) convertView.getTag();
			}
			MyAdapter myadapter = new MyAdapter(context, lineOrderBeans
					.getList().get(position).getProcessed_order_items());
			createtime = lineOrderBeans.getList().get(position)
					.getCreate_time();
			holder.expandlist = (MyListView) convertView
					.findViewById(R.id.expandlist);
			holder.create_time.setText(context.getString(R.string.createTime)
					+":"+ TimeFormat.DateFormat(createtime + ""));
			holder.expandlist.setAdapter(myadapter);
			// holder.delete_tv.setTag(position);
			// holder.delete_tv.setOnClickListener(this);
			return convertView;
		}

	}

	class viewHolder {
		LinearLayout cotainer;
		TextView create_time, delete_tv;
		MyListView expandlist;
	}

	class MyAdapter extends BaseAdapter {
		Context context;
		List<OfferLineOrderBeans.IdCCCC.Items> items;

		public MyAdapter(Context context,
				List<OfferLineOrderBeans.IdCCCC.Items> items) {

			// TODO Auto-generated constructor stub
			this.context = context;
			this.items = items;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub

			return items.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return items.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			viewMyListViewHolder myListViewHolder = null;
			if (convertView == null) {
				myListViewHolder = new viewMyListViewHolder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.offline_childview, null);
				convertView.setTag(myListViewHolder);
				LinearLayout childview_lin = (LinearLayout) convertView
						.findViewById(R.id.childview_lin);
				myListViewHolder.area = (TextView) convertView
						.findViewById(R.id.area);
				myListViewHolder.name = (TextView) convertView
						.findViewById(R.id.name);
				childview_lin.removeAllViews();
				for (int i = 0; i < items.get(position).getItems().size(); i++) {
					View view = LayoutInflater.from(context).inflate(
							R.layout.offline_childview_item, null);
					ImageView img = (ImageView) view.findViewById(R.id.img);
					TextView goods_name = (TextView) view
							.findViewById(R.id.goods_name);
					TextView goods_context = (TextView) view
							.findViewById(R.id.goods_context);
					LinearLayout lineyout = (LinearLayout) convertView
							.findViewById(R.id.lineyout);
					XImageLoader.load(
							GlobalConfig.IMAGE_DOUBLE
									+ items.get(position).getItems().get(i)
											.getId(), img);
					goods_name.setText(items.get(position).getItems().get(i)
							.getName());
					goods_context.setText(items.get(position).getItems().get(i)
							.getSecondary());
					childview_lin.addView(view);
				}
			} else {
				myListViewHolder = (viewMyListViewHolder) convertView.getTag();
			}
			myListViewHolder.area.setText(context.getString(R.string.placeOrigin)+"："
					+ items.get(position).getPlace_origin());
			myListViewHolder.name.setText(context.getString(R.string.name)+"："
					+ items.get(position).getContact_name() + "");
			return convertView;
		}
	}

	class viewMyListViewHolder {
		TextView name, area;
		LinearLayout lineyout;

	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		type = "clean";
		cartPresenter.onShopCharOrder(IsMobileNumPhone.isUid(), page_size+"", "1");
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		type = "load";
		page_size++;
		cartPresenter.onShopCharOrder(IsMobileNumPhone.isUid(), page_size+"",
				page_index + "");
	}

	void stop() {
		listview.stopLoadMore();
		listview.stopRefresh();
	}
}
